package com.example.newstest;

import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.widget.RadioGroup;


import com.example.newstest.comment.CommentFragment;
import com.example.newstest.databinding.ActivityMainBinding;
import com.example.newstest.news.fragment.NewsFragment;
import com.example.newstest.pic.PicFragment;
import com.example.newstest.test.TestFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private FragmentTransaction fragmentTransaction;
    private List<Fragment> fragments;
    private FragmentManager manager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        aboutFragment();
        aboutRadioGroup();
    }


    private void aboutRadioGroup() {
        binding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                fragmentTransaction = manager.beginTransaction();
                int id = -1;
                switch (checkedId) {
                    case R.id.main_news_but:
                        id=0;
                        fragmentTransaction.show(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(3)).commit();
                        break;
                    case R.id.main_test_but:
                        id=1;
                        fragmentTransaction.show(fragments.get(1)).hide(fragments.get(0)).hide(fragments.get(2)).hide(fragments.get(3)).commit();
                        break;
                    case R.id.main_pic_but:
                        id=2;
                        fragmentTransaction.show(fragments.get(2)).hide(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(3)).commit();
                        break;
                    case R.id.main_comment_but:
                        id=3;
                        fragmentTransaction.show(fragments.get(3)).hide(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(2)).commit();
                        break;
                }
//                for (int i = 0; i < fragments.size(); i++) {
//                    if(i==id){
//                        fragmentTransaction.show(fragments.get(i));
//                    }else{
//                        fragmentTransaction.hide(fragments.get(i));
//                    }
//                }
                //fragmentTransaction.commit();
            }
        });
    }

    private void aboutFragment() {
        manager = getSupportFragmentManager();
        fragmentTransaction = manager.beginTransaction();
        fragments = new ArrayList<>();
        NewsFragment newsFragment = new NewsFragment();
        TestFragment testFragment = new TestFragment();
        PicFragment picFragment = new PicFragment();
        CommentFragment commentFragment = new CommentFragment();
        fragments.add(newsFragment);
        fragments.add(testFragment);
        fragments.add(picFragment);
        fragments.add(commentFragment);
        fragmentTransaction.add(R.id.fragment_container, fragments.get(0)).show(fragments.get(0))
                .add(R.id.fragment_container, fragments.get(1)).hide(fragments.get(1))
                .add(R.id.fragment_container, fragments.get(2)).hide(fragments.get(2))
                .add(R.id.fragment_container, fragments.get(3)).hide(fragments.get(3)).commit();
    }
}
